<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"mathieulin.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="1. vue项目的安装1.1 依赖环境Vue CLI 4.x 需要 Node.js v8.9 或更高版本 (推荐 v10 以上)。 1.2 安装指令1npm install -g @vue&#x2F;cli  1.3 检查指令1vue --version   1.4 创建项目1vue create frontend  1.5 运行项目1npm run serve  运行命令后可在http:localhost">
<meta property="og:type" content="article">
<meta property="og:title" content="前后端交互">
<meta property="og:url" content="https://mathieulin.gitee.io/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/index.html">
<meta property="og:site_name" content="Mathieu&#39;s blog">
<meta property="og:description" content="1. vue项目的安装1.1 依赖环境Vue CLI 4.x 需要 Node.js v8.9 或更高版本 (推荐 v10 以上)。 1.2 安装指令1npm install -g @vue&#x2F;cli  1.3 检查指令1vue --version   1.4 创建项目1vue create frontend  1.5 运行项目1npm run serve  运行命令后可在http:localhost">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mathieulin.gitee.io/.io//vue_version.png">
<meta property="article:published_time" content="2022-06-02T05:14:37.000Z">
<meta property="article:modified_time" content="2022-06-02T12:05:27.601Z">
<meta property="article:author" content="Mathieu Lin">
<meta property="article:tag" content="前后端">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mathieulin.gitee.io/.io//vue_version.png">


<link rel="canonical" href="https://mathieulin.gitee.io/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://mathieulin.gitee.io/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/","path":"2022/06/02/前后端交互/","title":"前后端交互"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>前后端交互 | Mathieu's blog</title>
  




<link rel="dns-prefetch" href="https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app">
  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Mathieu's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Mathieu's blog</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">lzhow的debug路程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-vue%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%AE%89%E8%A3%85"><span class="nav-number">1.</span> <span class="nav-text">1. vue项目的安装</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E4%BE%9D%E8%B5%96%E7%8E%AF%E5%A2%83"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 依赖环境</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E5%AE%89%E8%A3%85%E6%8C%87%E4%BB%A4"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 安装指令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E6%A3%80%E6%9F%A5%E6%8C%87%E4%BB%A4"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 检查指令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.4.</span> <span class="nav-text">1.4 创建项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E8%BF%90%E8%A1%8C%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.5.</span> <span class="nav-text">1.5 运行项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-6-%E5%AE%89%E8%A3%85axios"><span class="nav-number">1.6.</span> <span class="nav-text">1.6 安装axios</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-7-%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82"><span class="nav-number">1.7.</span> <span class="nav-text">1.7 发送请求</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-Express%E9%A1%B9%E7%9B%AE"><span class="nav-number">2.</span> <span class="nav-text">2.Express项目</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-%E5%AE%89%E8%A3%85Express"><span class="nav-number">2.1.</span> <span class="nav-text">2.1 安装Express</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E5%AE%89%E8%A3%85express%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%94%9F%E6%88%90%E5%99%A8"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 安装express应用程序生成器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-%E5%88%9B%E5%BB%BA%E5%90%8D%E4%B8%BAbackend%E7%9A%84Express%E5%BA%94%E7%94%A8"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 创建名为backend的Express应用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-%E6%8C%89%E7%85%A7%E6%8F%90%E7%A4%BA%E8%BF%9B%E8%A1%8C%E6%93%8D%E4%BD%9C"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 按照提示进行操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-5-%E6%8F%90%E4%BE%9B%E6%8E%A5%E5%8F%A3%E8%AF%B7%E6%B1%82"><span class="nav-number">2.5.</span> <span class="nav-text">2.5 提供接口请求</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#routes%E4%B8%8B%E6%96%B0%E5%BB%BAproduct-js"><span class="nav-number">2.5.1.</span> <span class="nav-text">routes下新建product.js</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#app-js%E8%AE%BE%E7%BD%AE%E8%B7%A8%E5%9F%9F%E8%AE%BF%E9%97%AE"><span class="nav-number">2.5.2.</span> <span class="nav-text">app.js设置跨域访问</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%8B%A5%E9%9C%80%E6%8F%90%E4%BE%9B%E5%85%B6%E4%BB%96%E6%8E%A5%E5%8F%A3%EF%BC%8C%E5%88%99%E5%8F%AA%E9%9C%80%E5%9C%A8route%E4%B8%8B%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AA-example-js%EF%BC%8C%E5%B9%B6%E4%B8%94%E5%9C%A8app-js%E4%B8%AD%E6%B7%BB%E5%8A%A0%E4%B8%8B%E9%9D%A2%E4%B8%A4%E5%8F%A5"><span class="nav-number">2.5.2.1.</span> <span class="nav-text">若需提供其他接口，则只需在route下新建一个(example.)js，并且在app.js中添加下面两句</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-6-%E8%BF%90%E8%A1%8C%E5%90%8E%E7%AB%AF"><span class="nav-number">2.6.</span> <span class="nav-text">2.6 运行后端</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E4%BA%A4%E4%BA%92"><span class="nav-number">3.</span> <span class="nav-text">3. 交互</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Mathieu Lin</p>
  <div class="site-description" itemprop="description">这是一个记载了lzhow传奇的一生的博客</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/mathieulin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;mathieulin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:19241032@buaa.edu.cn" title="E-Mail → mailto:19241032@buaa.edu.cn" rel="noopener" target="_blank"><i class="fab fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://121.199.35.110/" title="Todo → http:&#x2F;&#x2F;121.199.35.110&#x2F;" rel="noopener" target="_blank">Todo</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://noietch.cn:88/" title="House → http:&#x2F;&#x2F;noietch.cn:88&#x2F;" rel="noopener" target="_blank">House</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" title="Douzero → https:&#x2F;&#x2F;vkceyugu.cdn.bspapp.com&#x2F;VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93&#x2F;0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" rel="noopener" target="_blank">Douzero</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="rss → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>rss</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mathieulin.github.io/myblog" title="Another-blog → https:&#x2F;&#x2F;mathieulin.github.io&#x2F;myblog" rel="noopener" target="_blank">Another-blog</a>
      </span>
  </div>



        </div>
      </div>
    </div>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1951671851&auto=1&height=66"></iframe>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mathieulin.gitee.io/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Mathieu Lin">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Mathieu's blog">
      <meta itemprop="description" content="这是一个记载了lzhow传奇的一生的博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="前后端交互 | Mathieu's blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          前后端交互
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2022-06-02 13:14:37 / 修改时间：20:05:27" itemprop="dateCreated datePublished" datetime="2022-06-02T13:14:37+08:00">2022-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/java/" itemprop="url" rel="index"><span itemprop="name">java</span></a>
        </span>
    </span>

  
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">评论数：</span>
  
    <a title="waline" href="/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-vue项目的安装"><a href="#1-vue项目的安装" class="headerlink" title="1. vue项目的安装"></a>1. vue项目的安装</h1><h2 id="1-1-依赖环境"><a href="#1-1-依赖环境" class="headerlink" title="1.1 依赖环境"></a>1.1 依赖环境</h2><p>Vue CLI 4.x 需要 Node.js v8.9 或更高版本 (推荐 v10 以上)。</p>
<h2 id="1-2-安装指令"><a href="#1-2-安装指令" class="headerlink" title="1.2 安装指令"></a>1.2 安装指令</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g @vue/cli</span><br></pre></td></tr></table></figure>

<h2 id="1-3-检查指令"><a href="#1-3-检查指令" class="headerlink" title="1.3 检查指令"></a>1.3 检查指令</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue --version</span><br></pre></td></tr></table></figure>

<p><img src="/.io//vue_version.png"></p>
<h2 id="1-4-创建项目"><a href="#1-4-创建项目" class="headerlink" title="1.4 创建项目"></a>1.4 创建项目</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vue create frontend</span><br></pre></td></tr></table></figure>

<h2 id="1-5-运行项目"><a href="#1-5-运行项目" class="headerlink" title="1.5 运行项目"></a>1.5 运行项目</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run serve</span><br></pre></td></tr></table></figure>

<p>运行命令后可在http:localhost:8000&#x2F;中看到初始化项目</p>
<h2 id="1-6-安装axios"><a href="#1-6-安装axios" class="headerlink" title="1.6 安装axios"></a>1.6 安装axios</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install axios</span><br></pre></td></tr></table></figure>

<h2 id="1-7-发送请求"><a href="#1-7-发送请求" class="headerlink" title="1.7 发送请求"></a>1.7 发送请求</h2><p>比如给按钮绑定了response方法，则在该vue的export default下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;App&#x27;</span>,</span><br><span class="line">    <span class="attr">components</span>: &#123;</span><br><span class="line">      </span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">methods</span>: &#123;</span><br><span class="line">      	<span class="title function_">response</span>(<span class="params"></span>)&#123;</span><br><span class="line">    		<span class="title class_">Axios</span>.<span class="title function_">get</span>(<span class="string">&quot;http://localhost:3000/product&quot;</span>).<span class="title function_">then</span>(<span class="keyword">function</span>(<span class="params">response</span>)&#123;</span><br><span class="line">          		<span class="variable language_">console</span>.<span class="title function_">log</span>(response);</span><br><span class="line">        	&#125;)	</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="2-Express项目"><a href="#2-Express项目" class="headerlink" title="2.Express项目"></a>2.Express项目</h1><h2 id="2-1-安装Express"><a href="#2-1-安装Express" class="headerlink" title="2.1 安装Express"></a>2.1 安装Express</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install express --save</span><br></pre></td></tr></table></figure>

<h2 id="2-2-安装express应用程序生成器"><a href="#2-2-安装express应用程序生成器" class="headerlink" title="2.2 安装express应用程序生成器"></a>2.2 安装express应用程序生成器</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install -g express-generator</span><br></pre></td></tr></table></figure>

<h2 id="2-3-创建名为backend的Express应用"><a href="#2-3-创建名为backend的Express应用" class="headerlink" title="2.3 创建名为backend的Express应用"></a>2.3 创建名为backend的Express应用</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">express --view=pug backend</span><br></pre></td></tr></table></figure>

<h2 id="2-4-按照提示进行操作"><a href="#2-4-按照提示进行操作" class="headerlink" title="2.4 按照提示进行操作"></a>2.4 按照提示进行操作</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">cd backend</span><br><span class="line">npm install</span><br><span class="line">SET DEBUG=backend:*</span><br><span class="line">npm start(建议在vscode中进行这一步)</span><br></pre></td></tr></table></figure>



<h2 id="2-5-提供接口请求"><a href="#2-5-提供接口请求" class="headerlink" title="2.5 提供接口请求"></a>2.5 提供接口请求</h2><h3 id="routes下新建product-js"><a href="#routes下新建product-js" class="headerlink" title="routes下新建product.js"></a>routes下新建product.js</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> router = express.<span class="title class_">Router</span>();</span><br><span class="line"><span class="comment">/* GET home page. */</span></span><br><span class="line">router.<span class="title function_">get</span>(<span class="string">&#x27;/&#x27;</span>, <span class="keyword">function</span>(<span class="params">req, res, next</span>) &#123;</span><br><span class="line">	<span class="keyword">var</span> data=&#123;</span><br><span class="line">		<span class="attr">code</span>:<span class="number">0</span>,</span><br><span class="line">		<span class="attr">data</span>:&#123;<span class="attr">name</span>:<span class="string">&#x27;aaa&#x27;</span>,<span class="attr">pwd</span>:<span class="string">&#x27;123&#x27;</span>&#125;,</span><br><span class="line">		<span class="attr">isSuccess</span>:<span class="literal">true</span>,</span><br><span class="line">		<span class="attr">msg</span>:<span class="string">&quot;请求成功&quot;</span></span><br><span class="line">	&#125;</span><br><span class="line">	res.<span class="title function_">json</span>(data);</span><br><span class="line">&#125;);</span><br><span class="line"> </span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = router;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="app-js设置跨域访问"><a href="#app-js设置跨域访问" class="headerlink" title="app.js设置跨域访问"></a>app.js设置跨域访问</h3><p>示例如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> createError = <span class="built_in">require</span>(<span class="string">&#x27;http-errors&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> cookieParser = <span class="built_in">require</span>(<span class="string">&#x27;cookie-parser&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> logger = <span class="built_in">require</span>(<span class="string">&#x27;morgan&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> indexRouter = <span class="built_in">require</span>(<span class="string">&#x27;./routes/index&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> usersRouter = <span class="built_in">require</span>(<span class="string">&#x27;./routes/users&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> productRouter = <span class="built_in">require</span>(<span class="string">&#x27;./routes/product&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> app = <span class="title function_">express</span>();</span><br><span class="line"></span><br><span class="line"><span class="comment">// view engine setup</span></span><br><span class="line">app.<span class="title function_">set</span>(<span class="string">&#x27;views&#x27;</span>, path.<span class="title function_">join</span>(__dirname, <span class="string">&#x27;views&#x27;</span>));</span><br><span class="line">app.<span class="title function_">set</span>(<span class="string">&#x27;view engine&#x27;</span>, <span class="string">&#x27;jade&#x27;</span>);</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">logger</span>(<span class="string">&#x27;dev&#x27;</span>));</span><br><span class="line">app.<span class="title function_">use</span>(express.<span class="title function_">json</span>());</span><br><span class="line">app.<span class="title function_">use</span>(express.<span class="title function_">urlencoded</span>(&#123; <span class="attr">extended</span>: <span class="literal">false</span> &#125;));</span><br><span class="line">app.<span class="title function_">use</span>(<span class="title function_">cookieParser</span>());</span><br><span class="line">app.<span class="title function_">use</span>(express.<span class="title function_">static</span>(path.<span class="title function_">join</span>(__dirname, <span class="string">&#x27;public&#x27;</span>)));</span><br><span class="line"></span><br><span class="line"><span class="comment">//设置跨域访问</span></span><br><span class="line">app.<span class="title function_">all</span>(<span class="string">&#x27;*&#x27;</span>, <span class="keyword">function</span> (<span class="params">req, res, next</span>) &#123;</span><br><span class="line">  res.<span class="title function_">header</span>(<span class="string">&quot;Access-Control-Allow-Origin&quot;</span>, <span class="string">&quot;*&quot;</span>);</span><br><span class="line">  res.<span class="title function_">header</span>(<span class="string">&quot;Access-Control-Allow-Headers&quot;</span>, <span class="string">&quot;X-Requested-With&quot;</span>);</span><br><span class="line">  res.<span class="title function_">header</span>(<span class="string">&quot;Access-Control-Allow-Methods&quot;</span>, <span class="string">&quot;PUT,POST,GET,DELETE,OPTIONS&quot;</span>);</span><br><span class="line">  res.<span class="title function_">header</span>(<span class="string">&quot;X-Powered-By&quot;</span>, <span class="string">&#x27; 3.2.1&#x27;</span>)</span><br><span class="line">  res.<span class="title function_">header</span>(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;application/json;charset=utf-8&quot;</span>);</span><br><span class="line">  <span class="title function_">next</span>();</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">&#x27;/&#x27;</span>, indexRouter);</span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">&#x27;/users&#x27;</span>, usersRouter);</span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">&#x27;/product&#x27;</span>, productRouter);</span><br><span class="line"></span><br><span class="line"><span class="comment">// catch 404 and forward to error handler</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="keyword">function</span> (<span class="params">req, res, next</span>) &#123;</span><br><span class="line">  <span class="title function_">next</span>(<span class="title function_">createError</span>(<span class="number">404</span>));</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// error handler</span></span><br><span class="line">app.<span class="title function_">use</span>(<span class="keyword">function</span> (<span class="params">err, req, res, next</span>) &#123;</span><br><span class="line">  <span class="comment">// set locals, only providing error in development</span></span><br><span class="line">  res.<span class="property">locals</span>.<span class="property">message</span> = err.<span class="property">message</span>;</span><br><span class="line">  res.<span class="property">locals</span>.<span class="property">error</span> = req.<span class="property">app</span>.<span class="title function_">get</span>(<span class="string">&#x27;env&#x27;</span>) === <span class="string">&#x27;development&#x27;</span> ? err : &#123;&#125;;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// render the error page</span></span><br><span class="line">  res.<span class="title function_">status</span>(err.<span class="property">status</span> || <span class="number">500</span>);</span><br><span class="line">  res.<span class="title function_">render</span>(<span class="string">&#x27;error&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = app;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="若需提供其他接口，则只需在route下新建一个-example-js，并且在app-js中添加下面两句"><a href="#若需提供其他接口，则只需在route下新建一个-example-js，并且在app-js中添加下面两句" class="headerlink" title="若需提供其他接口，则只需在route下新建一个(example.)js，并且在app.js中添加下面两句"></a>若需提供其他接口，则只需在route下新建一个(example.)js，并且在app.js中添加下面两句</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> exampleRouter = <span class="built_in">require</span>(<span class="string">&#x27;./routes/example&#x27;</span>);</span><br><span class="line">app.<span class="title function_">use</span>(<span class="string">&#x27;/example&#x27;</span>, exampleRouter);</span><br></pre></td></tr></table></figure>



<h2 id="2-6-运行后端"><a href="#2-6-运行后端" class="headerlink" title="2.6 运行后端"></a>2.6 运行后端</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm start</span><br></pre></td></tr></table></figure>





<h1 id="3-交互"><a href="#3-交互" class="headerlink" title="3. 交互"></a>3. 交互</h1><p>打开前端网站，点击按钮，然后就可以在控制台上看到请求值了</p>

    </div>

    
    
    

    <footer class="post-footer"><script src="//sdk.jinrishici.com/v2/browser/jinrishici.js"></script>
<script>
  jinrishici.load((result) => {
    let jrsc = document.getElementById('jrsc');
    const data = result.data;
    let author = data.origin.author;
    let title = '《' + data.origin.title + '》';
    let content = data.content.substr(0, data.content.length - 1);
    let dynasty = data.origin.dynasty.substr(0, data.origin.dynasty.length - 1);
    jrsc.innerText = content + ' @ ' + dynasty + '·' + author + title;
  });
</script>
<div style="text-align: center"><span id="jrsc" >正在加载今日诗词....</span></div>

          <div class="post-tags">
              <a href="/tags/%E5%89%8D%E5%90%8E%E7%AB%AF/" rel="tag"># 前后端</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/06/01/%E5%90%8E%E7%AB%AF%E9%83%A8%E7%BD%B2/" rel="prev" title="后端项目傻逼式部署">
                  <i class="fa fa-chevron-left"></i> 后端项目傻逼式部署
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/06/02/hello-world/" rel="next" title="Hello World">
                  Hello World <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mathieu Lin</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div><script color="0,0,255" opacity="0.5" zIndex="-1" count="99" src="https://cdnjs.sourcegcdn.com//ajax/libs/canvas-nest.js/1.0.1/canvas-nest.js"></script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-CN","enable":true,"serverURL":"https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"el":"#waline","comment":true,"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","path":"/2022/06/02/%E5%89%8D%E5%90%8E%E7%AB%AF%E4%BA%A4%E4%BA%92/"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
